<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>

    <meta charset="UTF-8">
    <title>物资入库</title>
    <script th:src="@{/js/jquery.min.js}"></script>
    <style>
        .greenStyle {
            background: lightgreen;
        }
    </style>
    <script>
        function queryMsg() {
            var fbarcode = document.getElementById("fbarcode").value;

            if (fbarcode == "" || fbarcode == null) {
                alert("物资条码不能为空！");
                return;
            }
            var stockinId=document.getElementById("stockinId").value;
            if (stockinId == "" || stockinId == null) {
                alert("单据号不能为空！");
                return;
            }

            $.post("/pda/tms/list", {
                stockinId:stockinId,
                fbarcode: fbarcode
            }, function (result) {
                console.log("1111111111111", result);
                if (result == null) {
                    alert("单据不存在");
                    return
                }
                if (result["status"] != null && result["status"] != 0) {
                    alert("此物资已提交信息")
                    return
                }
                var productId = result["nowMatnr"]
                var data = result["matnrList"]
                $('#matnr').val(productId)
                var quantity = result["quantity"];
                $('#quantity').val(quantity);
                if (data != null) {
                    var str = "";
                    for (var i = 0; i < data.length; i++) {
                        console.log(data[i].matnr)
                        if (!existProduct(data[i].matnr)) {
                            str = "<tr><td>" + data[i].matnr + "</td><td>" + data[i].maktx + "</td><td>" + data[i].ykutxt + "</td><td>"
                                + data[i].ewbze + "</td><td>" + data[i].yyantxt + "</td><td>" + data[i].funit + "</td><td>" + data[i].fpackunit + "</td><td>" + data[i].fnum + "</td><td>" + data[i].fflag + "</td></tr>";
                            $("#detailMsg1").append(str);
                        }

                    }

                } else {
                    alert("物资不存在，请核对！");
                }
            }, "json");

        }

        function existProduct(id) {
            var rows = document.getElementById("detailMsg1").rows.length - 1;
            for (var i = 0; i < rows; i++) {
                var selectRow = document.getElementById("detailMsg1").rows[i + 1];
                console.log(selectRow)
                if (selectRow.cells[0].innerHTML == id)
                    return true;
            }
            return false;
        }

        function saveInJob() {
            var fbarcode = document.getElementById("fbarcode").value;
            var trayCode = document.getElementById("trayCode").value;
            var quantity = document.getElementById("quantity").value;
            var cargoSpaceId=document.getElementById("cargoSpaceId").value;
            if (quantity == "") {
                alert("数量为空");
                return
            }
            if (fbarcode == "") {
                alert("物资条码为空");
                return
            }
            if (trayCode == "") {
                alert("托盘条码为空");
                return
            }
            if (cargoSpaceId == "") {
                alert("货位条码为空");
                return
            }
            $.ajax({
                "url": "/pda/saveJobP?trayCode=" + trayCode + "&fbarcode=" + fbarcode+"&cargoSpaceId="+cargoSpaceId
                    + "&quantity=" + quantity,
                "type": "POST",
                "dataType": "json",
                "success": function (result) {
                    console.log(result);
                    alert(result.msg)


                    $('#quantity').val("");
                    // $('#trayCode').attr("readonly", "readonly");
                    $('#cargoSpaceId').attr("readonly", "readonly");
                    $('#fbarcode').val("");
                    $('#matnr').val("");


                    var rows = document.getElementById("detailMsg1").rows.length;
                    if (rows > 1) {
                        var tab2 = document.getElementById("detailMsg1");
                        for (var j = rows; j > 1; j--) {
                            tab2.deleteRow(j - 1);
                        }
                    }

                }
            });
        }


        function sureIn() {
            var trayCode = document.getElementById("trayCode").value;
            if (trayCode == "") {
                alert("料箱码不能为空")
                return
            }
            $.ajax({
                "url": "/pda/sureInP?trayCode=" + trayCode,
                "type": "POST",
                "dataType": "json",
                "success": function (result) {
                    console.log(result);
                    if (result.msg == 1) {
                        alert("此料箱没有确认信息！");
                    } else if (result.msg == 2) {
                        alert("确认入库成功！");
                        $('#trayCode').removeAttr("readonly");
                        $('#cargoSpaceId').removeAttr("readonly");
                        $('#trayCode').val("");
                        $('#cargoSpaceId').val("");
                        $('#quantity').val("");
                        $('#fbarcode').val("");
                        $('#matnr').val("");

                        location.reload();
                    }
                }
            });
        }



    </script>
</head>
<body>
<table id="tbb" width="98%" border="1" align="center" cellPadding="0" cellSpacing="0">
    <tr>
        <td align="right">采购订单号：
        </td>
        <td>
            <input type="text" id="stockinId" name="stockinId" style="width:100%;color: #0d8ddb">
        </td>
    </tr>
    <tr>
        <td align="right">货位条码：
        </td>
        <td>
            <input type="text" id="cargoSpaceId" name="cargoSpaceId" style="width:100%;color: #0d8ddb">
        </td>
    </tr>


    <tr>
        <td align="right">托盘条码：
        </td>
        <td>
            <input type="text" id="trayCode" name="trayCode" style="width:100%;color: #0d8ddb">
        </td>
    </tr>

    <tr>
        <td align="right">子件条码:</td>
        <td><input id="fbarcode" type="text" name="fbarcode" style="width: 100%"/>
            <button id="btn" type="button" onclick="queryMsg()" style="width: 20%">查询</button>
        </td>

    </tr>
    <tr>
        <td align="right">物资编码：</td>

        <td><input id="matnr" type="text" name="matnr" style="width: 100%"></td>
    </tr>
    <tr>
        <td align="left" colspan="2">
            <table id="detailMsg1" name="detailMsg1">
                <tbody>
                <tr>
                    <td>物资编码</td>
                    <td>物资名称</td>
                    <td>款式</td>
                    <td>规格</td>
                    <td>颜色</td>
                    <td>基本单位</td>
                    <td>包装单位</td>
                    <td>子件总数</td>
                    <td>总序列数</td>
                </tr>
                </tbody>
            </table>
        </td>
    </tr>
    <tr>
        <td align="right">
            数量：
        </td>
        <td><input type="text" id="quantity" name="quantity"></td>
    </tr>


    <tr>
        <td height="25" bgcolor="#d9e8fc" align="center" colSpan="2">
            <table width="100%">
                <tr>
                    <td width="60%" align="center">

                        <button id="btn_submit2" type="button" onclick="saveInJob()" style="width:30%;"> 提交信息</button>
                    </td>
                    <td width="40%" align="center">
                        <button id="btn_submit" type="button" onclick="sureIn()" style="width:30%">确认入库</button>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
</body>
</html>